package com.duojuhe.common.utils.iputil;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;


/**
 * @ClassName: IPUtil
 * @Desc: Ip工具类
 * @date 2013年4月1日
 */
@Slf4j
public class IPUtils {

    //系统默认ip
    private final static String DEFAULT_IP = "127.0.0.1";

    /**
     * 获取ip地址
     * @param request
     * @return
     */
    public static String getClientIP(HttpServletRequest request) {
        try {
            String fromSource = "x-real-ip";
            String ip = request.getHeader("x-real-ip");
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("x-forwarded-for");
                fromSource = "x-forwarded-for";
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("proxy-client-ip");
                fromSource = "proxy-client-ip";
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("wl-proxy-client-ip");
                fromSource = "wl-proxy-client-ip";
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
                fromSource = "request.getRemoteAddr";
            }
            log.info("get client ip, ip = {}, source = {}", ip, fromSource);
            if(StringUtils.isNotEmpty(ip)){
                if(ip.indexOf(",")>0){
                    ip = ip.split(",")[0];
                }
            }
            return ip;
        } catch (Exception e) {
            log.error("获取ip地址出现异常",e);
            return DEFAULT_IP;
        }
    }

    /**
     * 网站后台专用获取ip地址【网站后台专用】
     *
     * @param request
     * @return
     */
    public static String getClientIPByIpHeader(HttpServletRequest request,String ipHeader) {
        try {
            if (StringUtils.isBlank(ipHeader)){
                return getClientIP(request);
            }
            //ip请求头
            for (String header : ipHeader.split(",")) {
                String ip = request.getHeader(header);
                if (StringUtils.isNotBlank(ip) && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip)) {
                    log.info("get admin client ip, ip = {}, source = {}", ip, header);
                    return ip;
                }
            }
            String ip = request.getRemoteAddr();
            if (StringUtils.isNotBlank(ip) && ip.length() > 0 && !"unknown".equalsIgnoreCase(ip)) {
                log.info("get admin client ip, ip = {}, source = {}", ip, "request.getRemoteAddr");
                return ip;
            }
            return DEFAULT_IP;
        } catch (Exception e) {
            log.error("获取ip地址出现异常:", e);
            return DEFAULT_IP;
        }
    }

    /**
     * 获取本地ip
     * @return
     */
    public static String getHostIp(){
        try{
            return InetAddress.getLocalHost().getHostAddress();
        }catch (UnknownHostException e){
            return DEFAULT_IP;
        }
    }

    /**
     * 获取地址
     * @return
     */
    public static String getHostName(){
        try{
            return InetAddress.getLocalHost().getHostName();
        }catch (UnknownHostException e){
            return "未知";
        }
    }

}

